package com.bbx.taxi.client.UpdateChecker.qihoo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.passanger.Return.Version;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Service.LoginService;
import com.bbx.taxi.client.Service.UpgradeService;
import com.bbx.taxi.client.UpdateChecker.ui.MyUpdateLoad;
import com.bbx.taxi.client.UpdateChecker.ui.MyUpdateLoad.UpdateType;
import com.bbx.taxi.client.Util.AppStarted;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickRightListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickSingleListener;
import com.yanxuwen.loadview.LoadingDialog;
import com.qihoo.appstore.updatelib.AppInfo;
import com.qihoo.appstore.updatelib.UpdateManager;
import com.qihoo.appstore.updatelib.UpdateManager.CheckUpdateListener;

/** 
 * @Path com.bbx.taxi.client.UpdateChecker.qihoo.MyQihooUpdate.java
 * @Author 严旭文
 * @Date 2016-4-27 上午11:39:35
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class MyQihooUpdate implements CheckUpdateListener {
	private Activity context;
	private LoadingDialog loaddialog;
	/** 不更新类型，0为启动页不更新的话直接调整，1为设置检查更新，不更新则弹出提示 */
	private UpdateType noupdatetype;
	 private AppInfo updateInfo;

	public interface OnQihooUpdateListener {
		/**

		 * 360开始执行更新或者取消更新

		 */
		public void onQihooUpdate();
	};

	public OnQihooUpdateListener mOnQihooUpdateListener = null;

	/**

	 * 检查版本更新,且监听

	 */
	public void setOnQihooUpdateListener(OnQihooUpdateListener l) {
		mOnQihooUpdateListener = l;
	}

	public MyQihooUpdate(Activity context, LoadingDialog loaddialog,
			UpdateType noupdatetype) {
		this.context = context;
		this.loaddialog = loaddialog;
		this.noupdatetype = noupdatetype;
		if (loaddialog != null && !loaddialog.isShowing()) {
			loaddialog.show();
		}
		
	}
	public void onStart(){
		 UpdateManager.setDebug(true);
		   UpdateManager.setTestMode(1);
		  UpdateManager.checkUpdate(context,false, false, AppStarted.getAppPackageName(context),
                  this);
	}

	/**

	 * 显示强制更新

	 */
	public void onShowForcedUpdate(String content, final String url,final String versiton) {
		final MyAlertDailog dialog = new MyAlertDailog(context);
		dialog.setTitle(context.getResources().getString(
				R.string.mydailog_update_content));
		dialog.setContent2(!content.equals("") ? content :"");
		dialog.setSingle(context.getResources().getString(
				R.string.mydailog_confirm_update));
		dialog.show();
		dialog.setOnClickSingleListener(new onClickSingleListener() {
			@Override
			public void onClickSingle() {
				dialog.dismiss();
				downloadApk(true,url, versiton);
			}
		});
	}
	/**

	 * 显示不强制更新

	 */
	public void onShowNoForcedUpdate(String content, final String url,final String versiton) {
		final MyAlertDailog dialog = new MyAlertDailog(context);
		dialog.setTitle(context.getResources().getString(
				R.string.mydailog_update_content));
		dialog.setContent2(!content.equals("") ? content :"");
		dialog.setLeftButtonText(context.getResources().getString(
				R.string.mydailog_cancel_update));
		dialog.setRightButtonText(context.getResources().getString(
				R.string.mydailog_confirm_update));
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
		dialog.setOnClickLeftListener(new onClickLeftListener() {
			@Override
			public void onClickLeft() {
				dialog.dismiss();
				mHandler.sendEmptyMessage(MSG_NEWEST);
			}
		});
		dialog.setOnClickRightListener(new onClickRightListener() {

			@Override
			public void onClickRight() {
				dialog.dismiss();
				downloadApk(false,url, versiton);
				if (mOnQihooUpdateListener != null)
					mHandler.sendEmptyMessage(MSG_NEWEST);
			}
		});
	}
	@Override
	public void onResult(boolean status, Bundle data) {
		if(!status){
			if (noupdatetype == UpdateType.TYPE_QIDONG) {
				mHandler.sendEmptyMessage(MSG_NEWEST);
			} else{
				ToastUtil.showToast(context, R.string.no_network);
			}
			return;
		}
		updateInfo = data.getParcelable(UpdateManager.KEY_UPDATE_INFO);
        boolean isNew=updateInfo.isNewVersion;
        if(isNew){
        	String change = updateInfo.updateComment;
        	String url = updateInfo.downloadUrl;
        	String versionName = updateInfo.versioName;
        	
        	MyMsg mMyMsg=new MyMsg();
    		mMyMsg.content=change;
    		mMyMsg.url=url;
    		mMyMsg.versition=versionName;
        	if (change != null
					&& change.contains(context
							.getString(R.string.forced_update))) {
        		
        		Message msg=new Message();
        		msg.what=MSG_DIALOG_FORCED;
        		msg.obj=mMyMsg;
        		mHandler.sendMessage(msg);
			} else {
				Message msg=new Message();
        		msg.what=MSG_DIALOG_NOFORCED;
        		msg.obj=mMyMsg;
        		mHandler.sendMessage(msg);
			}
        }else{
        	if (noupdatetype == UpdateType.TYPE_QIDONG) {
        		mHandler.sendEmptyMessage(MSG_NEWEST);
			} else if (noupdatetype == UpdateType.TYPE_SET) {
				mHandler.sendEmptyMessage(MSG_NEWEST_DIALOG);
			}
        }
	}
	public void downloadApk(boolean Forced,String url,String versiton){
		MyUpdateLoad mMyUpdateLoad=new MyUpdateLoad();
		mMyUpdateLoad.downloadApk(context, Forced,noupdatetype,url,versiton);
	}
	
	public static final int MSG_DIALOG_FORCED=1;//强制对话框
	public static final int MSG_DIALOG_NOFORCED=2;//不强制对话框
	public static final int MSG_NEWEST_DIALOG=3;//最新版本,有弹窗
	public static final int MSG_NEWEST=4;//最新版本，直接跳过
	
	public Handler mHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if (loaddialog != null)
				loaddialog.dismiss();
			switch(msg.what){
			case MSG_DIALOG_FORCED:
				MyMsg mMyMsg=(MyMsg) msg.obj;
				onShowForcedUpdate(mMyMsg.content, mMyMsg.url, mMyMsg.versition);
				break;
            case MSG_DIALOG_NOFORCED:
            	MyMsg mMyMsg2=(MyMsg) msg.obj;
            	onShowNoForcedUpdate(mMyMsg2.content, mMyMsg2.url, mMyMsg2.versition);
				break;
            case MSG_NEWEST_DIALOG:
        		final MyAlertDailog dialog = new MyAlertDailog(context);
				dialog.setTitle(context.getString(R.string.mydailog_title));
				dialog.setContent(context
						.getString(R.string.mydailog_update_latest));
				dialog.setSingle(context.getString(R.string.mydailog_confirm));
				dialog.show();
				dialog.setOnClickSingleListener(new onClickSingleListener() {
					@Override
					public void onClickSingle() {
						dialog.dismiss();
						mHandler.sendEmptyMessage(MSG_NEWEST);
					}
				});
            	break;
			case MSG_NEWEST:
				if (mOnQihooUpdateListener != null)
					mOnQihooUpdateListener.onQihooUpdate();
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	public class MyMsg{
		public String content;
		public String url;
		public String versition;
	}
}